from django.shortcuts import render, redirect
from .forms import RegistrationForm
from .models import UserProfile

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            # 创建用户对象
            user = User.objects.create_user(
                username=form.cleaned_data['username'],
                password=form.cleaned_data['password'],
                email=form.cleaned_data['email']
            )

            # 创建UserProfile对象并保存
            UserProfile.objects.create(user=user)

            # 可以根据需要执行其他操作，例如登录用户等

            return redirect('registration_success')  # 注册成功后重定向到成功页面

    else:
        form = RegistrationForm()

    return render(request, 'registration/register.html', {'form': form})
